package cn.shop.merchant.service;

import cn.shop.merchant.mapper.media.NoticeMapper;
import cn.shop.merchant.model.media.Notice;
import cn.shop.merchant.req.MediaQueryReq;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author : 李一涵
 * @date : Created in 2021-11-4 10:00
 * @description : 公告service
 */
@Service
public class NoticeService extends BaseServiceImpl<NoticeMapper, Notice> {

    /**
     * 获取公告列表
     *
     * @param uid 商户编号
     * @param req 查询条件
     */
    public Map<String, Object> list(long uid, MediaQueryReq req) {
        Page<Notice> page = Page.of(req.getCurrent(), req.getSize());
        LambdaQueryWrapper<Notice> query = Wrappers.<Notice>lambdaQuery()
                .eq(Notice::getUid, uid)
                .apply(notNull.test(req.getStartTime()), req.gtTime("create_time"), req.getStartTime())
                .apply(notNull.test(req.getEndTime()), req.ltTime("create_time"), req.getEndTime())
                .like(notNull.test(req.getTitle()), Notice::getTitle, req.getTitle())
                .orderByDesc(Notice::getId);
        page = page(page, query);

        return PageUtil.formatPage(page, this::format);
    }

    /**
     * 更新公告
     *
     * @param notice 公告信息
     * @param uid 商户编号
     */
    public boolean update(Notice notice, long uid) {
        return update(notice, Wrappers.<Notice>lambdaUpdate()
                .eq(Notice::getId, notice.getId())
                .eq(Notice::getUid, uid));
    }

    /**
     * 删除公告
     *
     * @param ids 编号，多个用逗号隔开
     * @param uid 商户编号
     */
    public boolean delete(List<Long> ids, long uid) {
        return SqlHelper.retBool(getBaseMapper().batchDeleteWithFill(new Notice(), Wrappers.<Notice>lambdaQuery()
                .in(Notice::getId, ids)
                .eq(Notice::getUid, uid)));
    }

    /**
     * 获取公告信息
     *
     * @param id 编号
     * @param uid 商户编号
     */
    public Map<String, Object> info(long id, long uid) {
        Notice notice = getOne(Wrappers.<Notice>lambdaQuery()
                .eq(Notice::getId, id).eq(Notice::getUid, uid));
        return format(notice);
    }

    /**
     * 格式化公告信息
     *
     * @param notice 公告信息
     */
    private Map<String, Object> format(Notice notice) {
        if (notice == null) {
            return Collections.emptyMap();
        }
        return Putif.start()
                .anyway("id", notice.getId())
                .anyway("title", notice.getTitle())
                //.anyway("author", notice.getAuthor())
                //.anyway("source", notice.getSource())
                .anyway("content", notice.getContent())
                .anyway("creatTime", notice.getCreateTime())
                .get();
    }

}
